package hu.berczi.FileProcessing;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileProcessing {

	public static final ArrayList<File> listFilesFrom(final File file) {
		return listFilesFrom(file, "*.*", true);
	}

	public static final ArrayList<File> listFilesFrom(final File file,
			String filter, boolean recursiv) {
		ArrayList<File> filesFound = new ArrayList<File>();

		if (file.isDirectory()) {
			if (recursiv) {
				final File[] childs = file.listFiles();
				for (File child : childs) {
					filesFound.addAll(listFilesFrom(child, filter, recursiv));
				}
			}
		} else {
			if (filter == null || filter.equals("")) {
				filesFound.add(file);
			} else {
				filter = filter.replaceAll("\\.", "[.]");
				filter = filter.replaceAll("\\*", ".*");
				Pattern pattern = Pattern.compile(filter);
				Matcher matcher = pattern.matcher(file.getName());
				if (matcher.find()) {
					filesFound.add(file);
				}
			}
		}
		return filesFound;
	}

}
